package a66weding.com.jiehuntong.fragment.category;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.andview.refreshview.XRefreshView;

import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.CategorySearchActivity;
import a66weding.com.jiehuntong.activity.ShopDetailsActivity;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.ShoplistBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.api.Url;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.common.utils.SharesUtils;
import a66weding.com.jiehuntong.common.utils.Utils;
import a66weding.com.jiehuntong.fragment.category.adapter.BusinessAdapter;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.inner.FragmentListener;
import a66weding.com.jiehuntong.inner.OnItemClickLitener;

/**
 * Created by admin on 2016/8/6.
 * 作用：搜索商家
 */
public class BusinessFragment extends Fragment implements OnNetResultListener, OnItemClickLitener, FragmentListener {
    private View view;
    private String content = "";
    private String city = "0";
    private int memberID;
    private RelativeLayout relativeLayout;
    private CategorySearchActivity categorySearchActivity;
    private XRefreshView xRefreshView;
    private RecyclerView recyclerView;
    private BusinessAdapter adapter;
    private boolean isDownRefresh = true;
    private int pageIndex = 1;
    private ShoplistBean bean;
    private List<ShoplistBean.DataBean> beanList;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        categorySearchActivity = (CategorySearchActivity) getActivity();
        view = inflater.inflate(R.layout.fragment_package, null);
        relativeLayout = (RelativeLayout) view.findViewById(R.id.package_nodata);
        initRefreshView(view);
        if (categorySearchActivity != null) {
            categorySearchActivity.setListener(this);
        }
        initData();
        return view;
    }

    @Override
    public void onDestroyView() {
        if (categorySearchActivity != null) {
            categorySearchActivity.removeListener(this);
        }
        super.onDestroyView();
    }

    public void initData() {
        memberID = DataWareHourse.getInstance().getmUser() != null ? DataWareHourse.getInstance().getmUser().getData().get(0).getI_Id() : 0;
        content = SharesUtils.getInstance(getContext()).readXML("search");
        if (!TextUtils.isEmpty(content)) {
            pageIndex = 1;
            getNetData(pageIndex);
        }
    }

    private void getNetData(int page) {
        String url = Url.GET_BUSINESSLISTSEARCH + "search=" + Utils.URLEncode(content) + "&city=" + city + "&pageIndex=" + page + "&memberID=" + memberID;
        HttpRequestBean RequestBean = new HttpRequestBean(RequestCode.GETREQUEST, url, this, ShoplistBean.class, RequestCode.GET_CASESLISTBYSEARCH);
        HttpHandleUtil.executeCommonGet(categorySearchActivity, RequestBean);
    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        if (isDownRefresh) {
            xRefreshView.stopRefresh();
        } else {
            xRefreshView.stopLoadMore();
        }
        Object data = sucessbean.getData();
        if (data instanceof ShoplistBean) {
            bean = (ShoplistBean) data;
            switch (bean.getResCode()) {
                case "0000":
                    beanList = bean.getData();
                    if (beanList.size() > 0) {
                        pageIndex++;
                    }
                    if (isDownRefresh) {
                        adapter.clear();
                    }
                    adapter.append(beanList);
                    if (adapter.getSize() == 0) {
                        relativeLayout.setVisibility(View.VISIBLE);
                        xRefreshView.setVisibility(View.INVISIBLE);
                    } else {
                        relativeLayout.setVisibility(View.INVISIBLE);
                        xRefreshView.setVisibility(View.VISIBLE);
                    }
                    break;
                default:
                    CUtils.showToast(bean.getMsg());
                    break;
            }
        }
    }

    @Override
    public void onError(ErrorBean errorBean) {
    }

    //初始化上啦刷新
    private void initRefreshView(View viewgroup) {
        xRefreshView = (XRefreshView) viewgroup.findViewById(R.id.category_xrefreshview);
        //是否允许下拉刷新
        xRefreshView.setPullRefreshEnable(true);
        // 设置是否可以上拉加载
        xRefreshView.setPullLoadEnable(true);
        // 设置刷新完成以后，headerview固定的时间
        xRefreshView.setPinnedTime(1000);
        // 静默加载模式不能设置footerview
        // 设置支持自动刷新
        xRefreshView.setAutoLoadMore(true);
        recyclerView = (RecyclerView) viewgroup.findViewById(R.id.category_recycler_view_test_rv);
        recyclerView.setHasFixedSize(true);
        //线性布局
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new BusinessAdapter(categorySearchActivity, null);
        adapter.setOnItemClickLitener(this);
        recyclerView.setAdapter(adapter);
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh() {
                isDownRefresh = true;
                pageIndex = 1;
                getNetData(pageIndex);
            }

            @Override
            public void onLoadMore(boolean isSlience) {
                isDownRefresh = false;
                ++pageIndex;
                getNetData(pageIndex);
            }
        });
    }
    @Override
    public void onItemClick(View view, int position) {
        Intent intent = new Intent();
        intent.setClass(categorySearchActivity, ShopDetailsActivity.class);
        intent.putExtra("nvc_num", beanList.get(position).getNvc_No());
        intent.putExtra("BusinessID", beanList.get(position).getI_Id());
        startActivity(intent);
    }
    @Override
    public void change(String content) {
        this.content = content;
        isDownRefresh = true;
        pageIndex = 1;
        getNetData(pageIndex);
    }
}
